package com.liming.common.handler;

import com.liming.common.exception.BusinessException;
import com.liming.common.exception.LoginException;
import com.liming.common.result.LMResult;
import feign.codec.DecodeException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@ResponseBody
@ControllerAdvice
public class GlobalExceptionHandler {


    @ExceptionHandler(BusinessException.class)
    // @ResponseStatus(HttpStatus.BAD_REQUEST) Http响应状态码
    public LMResult<Object> businessException(BusinessException ex) {
        log.error("业务异常：{}", ex.getMessage());
        return LMResult.error(ex.getMessage());
    }

    @ExceptionHandler(LoginException.class)
    public LMResult<Object> handleLoginException(LoginException ex) {
        log.error("登录异常：{}", ex.getMessage());
        return LMResult.error(ex.getMessage());
    }

    @ExceptionHandler(DecodeException.class)
    public LMResult<Object> decodeException(DecodeException ex) {
        log.error("Feign解码异常：{}", ex.getMessage());
        return LMResult.error(ex.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public LMResult<Object> handleException(Exception ex) {
        log.error("系统异常：{}", ex.getMessage());
        return LMResult.error(ex.getMessage());
    }
}
